import menuService from "@/service/menu.service";
import authMiddleware from "@/middleware/auth.middleware";
import { Context, Next } from "koa";
import { body, middlewares, routeConfig, z } from "koa-swagger-decorator";

// 添加菜单 Schema
const addSchema = z.object({
  menu_name: z.string(),
  menu_type: z.coerce.number(),
  path: z.string().nullable().optional(),
  component: z.string().nullable().optional(),
  perms: z.string().nullable().optional(),
  icon: z.string().nullable().optional(),
  status: z.coerce.number().nullable().optional(),
  parent_id: z.coerce.number().nullable().optional(),
});

// 更新菜单 Schema
const updateSchema = z.object({
  id: z.coerce.number(),
  menu_name: z.string().nullable().optional(),
  menu_type: z.coerce.number().nullable().optional(),
  path: z.string().nullable().optional(),
  component: z.string().nullable().optional(),
  perms: z.string().nullable().optional(),
  icon: z.string().nullable().optional(),
  status: z.coerce.number().nullable().optional(),
  parent_id: z.coerce.number().nullable().optional(),
});

// 分页查询 Schema
const getListSchema = z.object({
  current: z.coerce.number().min(1).default(1),
  pageSize: z.coerce.number().min(1).default(10),
  menu_name: z.string().nullable().optional(),
  menu_type: z.coerce.number().nullable().optional(),
  status: z.coerce.number().nullable().optional(),
});

export class MenuController {
  @routeConfig({
    method: "post",
    path: "/menu/add",
    summary: "添加菜单",
    tags: ["菜单"],
  })
  @body(addSchema)
  @middlewares([authMiddleware.verifyAuth])
  async add(ctx: Context, next: Next) {
    try {
      const res = await menuService.add(ctx.request.body as API.menuItem);
      ctx.status = 200;
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
    } catch (error: any) {
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "添加失败",
      };
      ctx.status = error.status || 400;
    }
  }

  @routeConfig({
    method: "delete",
    path: "/menu/delete",
    summary: "批量删除菜单",
    tags: ["菜单"],
  })
  @body(
    z.object({
      ids: z.array(z.number()),
    })
  )
  @middlewares([authMiddleware.verifyAuth])
  async delete(ctx: Context, next: Next) {
    try {
      const res = await menuService.delete(
        (ctx.request.body as { ids: number[] }).ids
      );
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
      ctx.status = 200;
    } catch (error: any) {
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "删除失败",
      };
      ctx.status = error.status || 400;
    }
  }

  @routeConfig({
    method: "put",
    path: "/menu/update",
    summary: "更新菜单",
    tags: ["菜单"],
  })
  @body(updateSchema)
  @middlewares([authMiddleware.verifyAuth])
  async update(ctx: Context, next: Next) {
    try {
      console.log(ctx.request.body);
      const res = await menuService.update(ctx.request.body as API.menuItem);
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
      ctx.status = 200;
    } catch (error: any) {
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "更新失败",
      };
      ctx.status = error.status || 400;
    }
  }

  @routeConfig({
    method: "get",
    path: "/menu/list",
    summary: "获取菜单列表",
    tags: ["菜单"],
    request: {
      query: getListSchema,
    },
  })
  @middlewares([authMiddleware.verifyAuth])
  async getList(ctx: Context, next: Next) {
    try {
      const res = await menuService.getList(
        ctx.request.query as unknown as API.menuList
      );
      ctx.body = {
        success: true,
        data: res,
        errorMessage: null,
      };
      ctx.status = 200;
    } catch (error: any) {
      ctx.body = {
        success: false,
        data: null,
        errorMessage: error.message || "获取列表失败",
      };
      ctx.status = error.status || 400;
    }
  }
}
